package lib_model.Account;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import lib_dal.Connection.ConnectMSSQL;
import lib_dal.Interface.ISql;
import lib_model.Orther.ReturnObj;

public class Account_factory implements ISql {
	public String username_ref;
	public int factory_id_ref;
	public String role;
	public Date date_create;

	public Account_factory() {
	}

	public Account_factory(String username_ref) {
		super();
		this.username_ref = username_ref;
	}

	@Override
	public ReturnObj Select() {
		String sql = "select username_ref, factory_id_ref, role, date_create from dbo.account_factory";
		ReturnObj obj = new ReturnObj();
		ArrayList<Account_factory> arr = new ArrayList<Account_factory>();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Account_factory g = new Account_factory();
				g.username_ref = rs.getString("username_ref");
				g.factory_id_ref = rs.getInt("factory_id_ref");
				g.role = rs.getString("role");
				g.date_create = rs.getDate("date_create");
				arr.add(g);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		obj.ObjResult = arr;
		return obj;
	}

	public ReturnObj SelectByIDfactory(int id) {
		String sql = "select username_ref, factory_id_ref, role, date_create from dbo.account_factory where factory_id_ref = "
				+ id;
		ReturnObj obj = new ReturnObj();
		ArrayList<Account_factory> arr = new ArrayList<Account_factory>();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Account_factory g = new Account_factory();
				g.username_ref = rs.getString("username_ref");
				g.factory_id_ref = rs.getInt("factory_id_ref");
				g.role = rs.getString("role");
				g.date_create = rs.getDate("date_create");
				arr.add(g);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		obj.ObjResult = arr;
		return obj;
	}

	@Override
	public ReturnObj Add() {
		String sql = "insert into dbo.account_factory  (username_ref,   factory_id_ref,   role,   date_create) values   (?,   ?,   ?,   GETDATE())";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setString(1, this.username_ref);
			preState.setInt(2, this.factory_id_ref);
			preState.setString(3, this.role);
			obj.Result = (preState.executeUpdate() != 0) ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Update() {
		String sql = "update dbo.account_factory set factory_id_ref = ?,   role = ?,   date_create = ? where  username_ref = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.factory_id_ref);
			preState.setString(2, this.role);
			preState.setDate(3, (java.sql.Date) this.date_create);
			preState.setString(4, this.username_ref);
			obj.Result = (preState.executeUpdate() != 0) ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Delete() {
		String sql = "delete from dbo.account_factory where username_ref = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setString(1, this.username_ref);
			obj.Result = preState.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj GetInformation() {
		ReturnObj obj = new ReturnObj();
		String sql = "select username_ref, factory_id_ref, role, date_create from dbo.account_factory where username_ref = ?";
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setString(1, this.username_ref);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Account_factory g = new Account_factory();
				g.username_ref = rs.getString("username_ref");
				g.factory_id_ref = rs.getInt("factory_id_ref");
				g.role = rs.getString("role");
				g.date_create = rs.getDate("date_create");
				obj.ObjResult = g;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

}
